package cn.christ.com.mobilesafe.view.activity_process.adapter;

import android.content.Context;
import android.graphics.Color;
import android.text.format.Formatter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

import cn.christ.com.mobilesafe.R;
import cn.christ.com.mobilesafe.common.utils.SPUtils;
import cn.christ.com.mobilesafe.constant.Constants;
import cn.christ.com.mobilesafe.modle.ProcessInfo;
import se.emilsjolander.stickylistheaders.StickyListHeadersAdapter;

/**
 * Created by Administrator on 2018/7/25.
 */

public class ProcessAdapter extends BaseAdapter implements StickyListHeadersAdapter {


    private Context context;
    private final List<ProcessInfo> sysInfos;
    private final List<ProcessInfo> userInfos;
    private boolean mIsChecked;

    public ProcessAdapter(Context context, List<ProcessInfo> sysInfos, List<ProcessInfo> userInfos) {
        this.context = context;
        this.sysInfos = sysInfos;
        this.userInfos = userInfos;
        mIsChecked = (boolean) SPUtils.get(Constants.SHOW_SYS, false);
    }

    @Override
    public int getCount() {
        if (mIsChecked) {
            return userInfos.size() + sysInfos.size();
        } else {
            return userInfos.size();
        }
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = View.inflate(context, R.layout.view_process_lv_item, null);
            convertView.setTag(holder);
            holder.ivProcess = (ImageView) convertView.findViewById(R.id.iv_process);
            holder.tvProcessName = (TextView) convertView.findViewById(R.id.tv_process_name);
            holder.tvProcessMemory = (TextView) convertView.findViewById(R.id.tv_process_memory);
            holder.cbProcess = (CheckBox) convertView.findViewById(R.id.cb_process);

        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        // 获取正在运行进程的信息，设置给UI
        // ProcessInfo processInfo = infos.get(position);
        ProcessInfo processInfo = getProcessInfo(position);
        holder.ivProcess.setImageDrawable(processInfo.icon);
        holder.tvProcessName.setText(processInfo.appName);
        holder.tvProcessMemory.setText(Formatter.formatFileSize(context, processInfo.usedMem));
        holder.cbProcess.setChecked(processInfo.isChecked);

        if (processInfo.isChecked) {
            holder.cbProcess.setChecked(true);
        } else {
            holder.cbProcess.setChecked(false);
        }

        // 假如是自己，不被刚掉
        // if (getPackageName().equals(processInfo.processName)) {
        // holder.cbProcess.setVisibility(View.GONE);
        // } else {
        // holder.cbProcess.setVisibility(View.VISIBLE);
        // }

        return convertView;
    }

    @Override
    public View getHeaderView(int position, View convertView, ViewGroup parent) {
        TextView tv = (TextView) View.inflate(context, R.layout.tv_app_head, null);
        tv.setTextColor(Color.BLACK);
        if (position < userInfos.size()) {
            // 返回用户进程头部的View
            tv.setText("用户进程");
        } else {
            // 返回系统进程头部的View
            tv.setText("系统进程");
        }
        return tv;
    }

    @Override
    public long getHeaderId(int position) {
        // 用户进程头部id:0
        // 系统进程头部id:1
        return position < userInfos.size() ? 0 : 1;
    }

    public class ViewHolder {
        ImageView ivProcess;
        TextView tvProcessName;
        TextView tvProcessMemory;
        CheckBox cbProcess;
    }

    private ProcessInfo getProcessInfo(int position) {
        ProcessInfo processInfo = null;
        if (position < userInfos.size()) {
            processInfo = userInfos.get(position);
        } else {
            processInfo = sysInfos.get(position - userInfos.size());
        }
        return processInfo;
    }
}
